package com.bgds.sys.module.controller;


import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.pagevo.SeriesVo;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lj
 */
@Slf4j
@RestController
@Api(tags = "油箱液位监测接口")
@RequestMapping("/tankLevel")
public class TankLevelController {

    @Autowired
    private IDeviceDynamicAttrService iDeviceDynamicAttrService;
    @Autowired
    private InfluxService influxService;
    @Autowired
    private IAlarmLogService iAlarmLogService;
    @RequestMapping(value = "/getChartData", method = RequestMethod.GET)
    @ApiOperation(value = "油箱液位监测压力、液位、温度图形分析数据",notes = "油箱液位监测压力、液位、温度图形分析数据",httpMethod = "GET",response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", value = "设备编码"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间")
    })
    public Result<Map<String,Object>> getDeviceChartData(String deviceCode,Long startTime,Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"sys");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        Map<String, List< DeviceDynamicAttr>> groupMap = deviceDynamicAttrs.stream().
                collect(
                        Collectors.groupingBy((DeviceDynamicAttr dda) -> dda.getGroupCode())
                );
        Map<String,List<SeriesVo>> chartObject=new HashMap<>(10);
        groupMap.forEach((k,v)->{
                //图表数据
                List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,v);
                chartObject.put(k,chartData);
        });
        map.put("chartList",chartObject);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }
}
